﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class EntityKeywordDomainService : IEntityKeywordDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, EntityKeyword> _repository;

        public EntityKeywordDomainService(IRepository<Guid, EntityKeyword> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public EntityKeyword Create(EntityKeyword keyword)
        {
            keyword.Id = Guid.NewGuid();
            return _repository.Create(keyword);
        }

        public EntityKeyword Get(Guid id)
        {
            return _repository.Get(id);
        }

        public EntityKeyword Update(EntityKeywordUpdateBo bo)
        {
            EntityKeyword entityKeyword = _repository.Get(bo.Id);
            //entityKeyword.Id = bo.Id;
            //entityKeyword.EntityId = bo.EntityId;
            entityKeyword.Content = bo.Content;
            entityKeyword.Synonyms = bo.Synonyms;

            _repository.Update(entityKeyword);

            return entityKeyword;

        }

        public IEnumerable<EntityKeyword> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            EntityKeyword entityKeyword = _repository.Get(id);

            _repository.Delete(entityKeyword);

        }

        public List<EntityKeyword> GetByEntityId(Guid entityId)
        {
            return _repository.List(new EntityKeywordFilterSpecification(entityId)).ToList();
        }
        public List<EntityKeyword> GetByBotId(Guid botId)
        {
            IEntityDomainService entityDomainService = _container.Resolve<IEntityDomainService>();
            var entityIds = entityDomainService.GetAllByBotId(botId).Select(a=>a.Id).ToList();
            return _repository.List(new EntityKeywordFilterSpecification(entityIds)).ToList();
        }
    }
}
